from rest_framework import serializers
from goods_info.models import GoodsCategoryModel


class CategorySerializers(serializers.ModelSerializer):
    '''
    商品类别的序列化器和反序列化
    '''
    # 树形展示
    children = serializers.SerializerMethodField(read_only=True)
    class Meta:
        model = GoodsCategoryModel
        fields = '__all__'
    # 函数的命名为get_属性名字
    def get_children(self,obj):  # obj当前的模型对象，GoodsCategoryModel对象
        if obj.children: # 获取当前类别下的子类别
            return CategorySerializers(obj.children,many=True).data
        else:
            return None